﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using snat.proxy.items;

namespace snat.proxy.sqlapi {
    public static class MessageQuery {

        public static ListProxy<Message> All() {
            Console.WriteLine("Requesting all");
            return Query.SQLQuery<snat.proxy.items.Message>(
                String.Join(" ", Query.SELECT, Query.ALL, Query.FROM, 
                                "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageProperties, 
                                        Query.NATURAL_JOIN, Query.MessageSend, ")",  
                                        Query.AS, "dummy", 
                                Query.NATURAL_JOIN, Query.Entity,
                                Query.ORDER_BY, Query.TimeStamp),
@"SELECT * FROM ( SELECT  MessageID, TimeStamp FROM  MessageProperties NATURAL JOIN MessageSend NATURAL JOIN Entity ORDER BY TimeStamp, MessageID LIMIT ?position, ?bufferSize) AS dummy NATURAL JOIN MessageReceive NATURAL JOIN Entity;
");

            //todo TODO imperative todo whack in full query which limits it tu
        }
     
        public static ListProxy<Message> SentBy(int id) {
            return Query.SQLQuery<Message>(String.Join(" ",
                    Query.SELECT, Query.ALL, Query.FROM, "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageSend, 
                        Query.NATURAL_JOIN, Query.MessageProperties, ")", Query.AS, "dummy",
                        Query.NATURAL_JOIN, Query.Entity,
                    Query.WHERE, Query.EntityID, Query.EQUALS, id));
        }

        public static ListProxy<Message> Sent() {
            return Query.SQLQuery<Message>(String.Join(" ",
                    Query.SELECT, Query.ALL, Query.FROM, "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageSend,
                        Query.NATURAL_JOIN, Query.MessageProperties, ")", Query.AS, "dummy",
                        Query.NATURAL_JOIN, Query.Entity));
        }

        public static ListProxy<Message> ReceivedBy(int id) {
            return Query.SQLQuery<Message>(String.Join(" ",
                    Query.SELECT, Query.ALL, Query.FROM, "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageReceive,
                        Query.NATURAL_JOIN, Query.MessageProperties, ")", Query.AS, "dummy",
                        Query.NATURAL_JOIN, Query.Entity,
                    Query.WHERE, Query.EntityID, Query.EQUALS, id));
        }

        public static ListProxy<Message> Received() {
            return Query.SQLQuery<Message>(String.Join(" ",
                    Query.SELECT, Query.ALL, Query.FROM, "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageReceive,
                        Query.NATURAL_JOIN, Query.MessageProperties, ")", Query.AS, "dummy",
                        Query.NATURAL_JOIN, Query.Entity));
        }

        public static Message FirstMessage() {
            return Query.ConstantQuery<Message>(String.Join(" ", 
                        Query.SELECT, Query.ALL, Query.FROM, 
                            "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageProperties, 
                                    Query.NATURAL_JOIN, Query.MessageSend, ")",  
                                    Query.AS, "dummy", 
                            Query.NATURAL_JOIN, Query.Entity, 
                            Query.ORDER_BY, Query.TimeStamp, Query.LIMIT, "1", ",", "1"));
        }
    
        public static Message LastMessage(bool isGreedy = true) {
            return Query.ConstantQuery<Message>(String.Join(" ",
                        Query.SELECT, Query.ALL, Query.FROM,
                            "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageProperties,
                                    Query.NATURAL_JOIN, Query.MessageSend, ")",
                                    Query.AS, "dummy",
                            Query.NATURAL_JOIN, Query.Entity,
                            Query.ORDER_BY, Query.TimeStamp, Query.DESC, Query.LIMIT, "1", ",", "1"));
        }
    }
}
